package ly.gupao.edu.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @author: liyang
 * @date：2018/5/28 0028
 * describe：session监听器， 设置session
 */
@WebListener
@ServletComponentScan
public class SessionAttributeListener implements HttpSessionListener,HttpSessionAttributeListener {

    private Logger logger = LoggerFactory.getLogger(SessionAttributeListener.class);

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        logger.debug("初始化springboot session 监听器！！！");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {

    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        if("test".equals(se.getName())){
            SystemSession.setParam((String) se.getValue());
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        SystemSession.removeParam();
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
        if("test".equals(se.getName())){
            SystemSession.setParam((String) se.getValue());
        }
    }


}
